<?php
	include_once 'isLogin.php';

	$table_name = "xinjian";// 操纵的表名字
	
	//添加
	if($_GET['action']=='add'){
		//插入数据
		$now = '1';//改的now
		$arr = array('content'=>$_POST['name'],'now'=>$now,'upid'=>$_POST['upid']);//改的now
		$commonc->insert($table_name,$arr);
		$id = $commonc->insert_id();
		
		$arr = array('sort'=>$id);
		$commonc->update($table_name, $arr, "id='".$id."'");
		
		
		$color=array('0'=>'#ff0000','1'=>'#16d316');
		$name=array('0'=>'下架','1'=>'上架');
		
		if($id!=''){
			$return='<tr>
			<td> <input type="checkbox" name="checkbox" value="'.$id.'" /></td>
			<td name_id="'.$id.'"> '.$_POST['name'].'</td>
			
			<td class="xiugai">
				<a href="javascript:;" onclick="sort($(this),\''.$id.'\',\'up\',\'common_test.php?action=sort\')"><img src="images/sort-up.png"/></a>
				<a href="javascript:;" onclick="sort($(this),\''.$id.'\',\'down\',\'common_test.php?action=sort\')"><img src="images/sort-down.png"/></a>
			</td>
			<td class="xiugai"><a href="javascript:;" style="color: '.$color[$now].'" onclick="state(\''.$id.'\',\'now\',\'common_test.php\',$(this))">'.$name[$now].'</a></td>
			<td class="xiugai">
			<a href="javascript:;" onclick="edit(\''.$id.'\',$(this),\'common_test.php?action=edit\')">修改</a>';
			if($_POST['upid']==false) $return.='<a href="common_test.php?id='.$id.'">子类</a>';
			$return.='</td>
			<td>&nbsp;</td>
			</tr>';
			$arr=array('error'=>'0','content'=>$return);
		    }else{
			$arr=array('error'=>'1','content'=>'添加失败');
		    }
		    echo json_encode($arr);
		    exit();
	}
	
	//删除
	if($_GET['action']=='del'){
		$delFile = array(
				array('key'=>'img','dir'=>'../upload/test/large/'),
				array('key'=>'img','dir'=>'../upload/test/middle/'),
				array('key'=>'img','dir'=>'../upload/test/small/'),
				array('key'=>'file','dir'=>'../upload/test/file/'),
		);
		echo $commonc->del($table_name,$_POST['id'],$delFile);
		exit();
	}
	
	//修改
	if($_GET['action']=='edit'){
		$arr = array('content'=>$_POST['name']);
		echo $commonc->update($table_name,$arr,"id='".$_POST['name_id']."'");
		exit();
	}
	
	//修改状态
	if($_GET['action']=='now'){
		if($_POST['key']=='now'){
			$color=array('0'=>'#ff5000','1'=>'#16c316');
			$name=array('0'=>'下架','1'=>'上架');
		}
		echo $commonc->changeState($table_name,$_POST['id'],$_POST['key'],$color,$name);
		exit();
	}
	
	//排序
	if($_GET['action']=='sort'){
		if($_POST['type']=='up') $type = "prev";
		if($_POST['type']=='down') $type = "next";
		$sort = $commonc->sortChange($table_name, $_POST['id'], $type);
		echo $sort['id'];
		exit();
	}

	//得到所有内容
	if($_GET['id']!=''){
		$where = " where upid='".$_GET['id']."'";
		$upid = $_GET['id'];
	}else{
		$upid = '0';
		$where = " where upid='0'";
	}
	$smarty->assign("upid",$upid);
	$sql = $table_name;
	$sql.= $where;
	$sql.= "order by sort desc";
	$row=$commonc->getList($sql,$sql);
	
	if(is_array($row['result'])){
		//得到状态
		$stateColor=array('0'=>'#ff0000','1'=>'#16d316');
		$stateName=array('0'=>'下架','1'=>'上架');
		$row['result'] = $commonc->getState($stateColor,$stateName,$row['result'],'now');
	}
	
	$smarty->assign("row",$row['result']);
	$smarty->assign('page',$row['page']);
	$smarty->display("common_text.html");
?>